
<template>
  <div> 
    <!--面包屑-->
    <bread :bread="$route.meta.bread" />
    
    <el-card>
      <!-- 表单区域 -->
      <el-form :inline="true">
        <el-form-item>
          <el-input placeholder="请输入搜索内容" v-model="query" clearable @clear="getGoodsData()">
            <!-- 搜索按钮 -->
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsData()"
            ></el-button>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="$router.push('/goods/add')"
            >添加商品</el-button
          >
        </el-form-item>
      </el-form>
      <!-- 列表区域 -->
      <el-table :data="goodsList" border style="width: 100%">
        <el-table-column label="#" width="80" type="index"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="140">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)"> </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量"> </el-table-column>
        <el-table-column prop="add_time" label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.add_time | toTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="delGood(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <div class="pages">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pagenum"
          :page-sizes="[2, 4, 6, 8]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totals"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import bread from './../bread'
export default {
  components: { bread },
  name: "",
  data() {
    return {
      query: "",
      pagenum: 1,
      pagesize: 2,
      totals: 0,
      goodsList: [], //商品列表数据
    };
  },
  created() {
    this.getGoodsData();
  },
  methods: {
    // 获取商品列表数据
    async getGoodsData() {
      let { data: res } = await this.$http.get(
        `goods?query=${this.query}&pagenum=${this.pagenum}&pagesize=${this.pagesize}`
      );
      // console.log(res);
      if(res.meta.status !== 200){
          return this.$message.error('获取商品列表数据失败');
      }
      this.goodsList = res.data.goods;
      this.totals = res.data.total;
    },
    // 分页
    handleSizeChange(size) {
      this.pagesize = size;
      this.getGoodsData();
    },
    handleCurrentChange(page) {
      this.pagenum = page;
      this.getGoodsData();
    },
    // 删除商品
    delGood(id) {
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let { data: res } = await this.$http.delete("goods/" + id);
          console.log(res);
          if(res.meta.status !== 200){
              return this.$message.error('删除商品失败');
          }
          this.$message.success(res.meta.msg);
          this.getGoodsData();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  //过滤器
  filters: {
    toTime(val) {
      return new Date(val)
        .toLocaleString()
        .replace(/\//g, "-")
        .replace(/(上午|下午)/, " ");
    },
  },
  computed: {},
  watch: {},
};
</script>

<style scoped>
.pages {
  margin-top: 15px;
}
</style>